<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 18-8-10
 * Time: 下午5:46
 */

namespace common\models\db;

use common\rewrite\deepziyu\yii_fast_api\ApiException;


/**
 * Class EquipmentOperateLog
 * @package common\models\db
 *
 * @property Equipment $equipment
 * @property Equipment $gEquipment
 * @property EquipmentUser $successForEquipmentUser
 */
class EquipmentOperateLog extends \common\models\db\tables\EquipmentOperateLog
{
    const OPERATE_NOT_DEFINED = 0;
    const OPERATE_SIGNUP_SEND_CODE = 1;
    const OPERATE_SIGNUP = 2;
    const OPERATE_LOGIN_SEND_CODE = 11;
    const OPERATE_LOGIN = 12;
    const RESET_PASSWORD_SEND_CODE = 21;
    const RESET_PASSWORD = 22;
    public static function getEquipmentOperateType()
    {
        return [
            self::OPERATE_NOT_DEFINED => '未定义的默认值',
            self::OPERATE_SIGNUP_SEND_CODE => '获取注册验证码',
            self::OPERATE_SIGNUP=> '注册',
            self::OPERATE_LOGIN_SEND_CODE => '获取登陆验证码',
            self::OPERATE_LOGIN => '登陆',
            self::RESET_PASSWORD_SEND_CODE => '获取重置密码验证码',
            self::RESET_PASSWORD => '重置密码',
        ];
    }
    public static function getEquipmentOperateTypesArr()
    {
        $arr = [];
        foreach (self::getEquipmentOperateType() as $k => $v){
            $arr[] = $k;
        }
        return $arr;
    }
    public static function checkEquipmentOperateTypeExist($opt_type, $is_throw_api_exception = 0)
    {
        if (in_array($opt_type, self::getEquipmentOperateTypesArr())){
            $bool = true;
        }else{
            $bool = false;
        }
        if ($is_throw_api_exception&&$bool==false){
            throw new ApiException(201808150955, '未找到设备操作类型');
        }
        return $bool;
    }

    /**
     * 设备用户是否可以进行该操作
     * @param $user
     * @param $opt_type
     * @param int $is_throw_api_exception
     * @return bool
     * @throws ApiException
     */
    public static function checkUserTypeCanDo($user, $opt_type, $is_throw_api_exception = 0)
    {
        $bool = true;
        if (in_array($opt_type, [EquipmentOperateLog::OPERATE_SIGNUP_SEND_CODE, EquipmentOperateLog::OPERATE_SIGNUP])){
            if ($user){
                $bool = false;
            }else{
                $bool = true;
            }
        }
        if ($is_throw_api_exception&&$bool==false){
            throw new ApiException(201808151153, '手机号已注册');
        }
        return $bool;
    }

    /**
     * 自定义记录设备操作日志
     * @param $uuid
     * @param $type
     * @param $app_version_id
     * @param array $other_data
     * @param null $for_equipment_user
     * @throws ApiException
     */
    public static function logEquipmentOperate($uuid, $type, $app_version_id, $other_data = [], $for_equipment_user = null)
    {
        $opt_log = new self();
        $opt_log->equipment_id = \Yii::$app->equipmentInfo->equipment->id;
        $opt_log->operate_type = $type;
        $params = ArrayHelper::merge(\Yii::$app->request->get(), \Yii::$app->request->post());
        if (isset($params['password']))unset($params['password']);
        $opt_log->req_data = json_encode($params);
        $opt_log->created_at = Yii_BT_TIME;
        $opt_log->req_mobile_pre = isset($other_data['req_mobile_pre'])?$other_data['req_mobile_pre']:'+86';
        $opt_log->req_mobile = isset($other_data['req_mobile'])?$other_data['req_mobile']:null;
        $opt_log->req_account = isset($other_data['req_account'])?$other_data['req_account']:null;
        $opt_log->req_username = isset($other_data['req_username'])?$other_data['req_username']:null;
        $opt_log->req_email = isset($other_data['req_email'])?$other_data['req_email']:null;
        $opt_log->resp_data = isset($other_data['resp_data'])?json_encode($other_data['resp_data'], JSON_UNESCAPED_UNICODE):null;
        if ($for_equipment_user){
            $e_user = EquipmentUser::findOne($for_equipment_user);
            if (!$e_user){
                throw new ApiException(201808151129, '没有找到用户绑定设备');
            }
            $opt_log->for_equipment_user = $for_equipment_user;
        }
        if (!$opt_log->save()){
            throw new ApiException(201808142021, '设备操作记录保存失败');
        }
    }

    private $_gEquipment;

    public function setGEquipment($equipment)
    {
        $this->_gEquipment = $equipment;
    }

    public function getGEquipment()
    {
        return $this->_gEquipment;
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getEquipment()
    {
        return $this->hasOne(Equipment::className(), ['id' => 'equipment_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getSuccessForEquipmentUser()
    {
        return $this->hasOne(EquipmentUser::className(), ['id' => 'success_for_equipment_user']);
    }
}